<!DOCTYPE html>
<html>
<head>
	<title>数据字典 Dictionaries </title>
</head>

<link rel="stylesheet" type="text/css"   href="style.css" />


<style  type="text/css">
 
</style>
<body>




<?php
// 启动会话  
session_start();  
header("Content-type:text/html;charset=utf-8");


// 检测PHP环境
if(version_compare(PHP_VERSION,'5.3.0','<'))  die('require PHP > 5.3.0 !');

// 开启调试模式 建议开发阶段开启 部署阶段注释或者设为false
define('APP_DEBUG',True);
$config = require( './config.php');
 
define('DB_TYPE' ,  $config['DB_TYPE'] ?? 'mysql');//, // 数据库类型
define('DB_PORT' ,  $config['DB_PORT'] ?? 3306);//, // 端口
define('DB_HOST' ,  $config['DB_HOST'] ?? '127.0.0.1');//, // 数据库类型
define('DB_NAME',   $config['DB_NAME'] ?? '');//, // 数据库名
define('DB_USER',   $config['DB_USER'] ?? '');//, // 用户名
define('DB_PWD' ,   $config['DB_PWD']  ?? '');//, // 密码
define('DB_PREFIX', $config['DB_PREFIX'] ?? '');//, // 后缀

 
 // router 
$ac = 'login';
if (isset( $_GET['ac'])  ) {  
	$ac = $_GET['ac'];
}

 


$ObjDictionaries = new Dictionaries();
$ObjDictionaries->$ac( );

// run();
//  if($ac === 'logout'){ 
//  	$ObjDictionaries->logout( );
// }else if($ac === 'login'){
// 	$ObjDictionaries->login( );
//  }else if($ac === 'login_sub'){
//  	$ObjDictionaries->login_sub( );
//  } else if($ac === 'index'){
//  	$ObjDictionaries->run();
// }
 

class Dictionaries{
	 public  $user_array = array('username' => 'dev' , 'password' => '654321dev' );
		public $time;// = time();
		public $dbh;
		public $keys = ['PRI'=>'主键','MUL'=>'普通的b-tree索引','UNI'=>'唯一索引' ];
		function __construct(){
			$this->time = time();
		}
		public  function generateCSRFToken() {  
		    // 你可以在这里添加生成令牌的逻辑  
		    $token = md5(uniqid(rand(), true));  
		    return $token;  
		}
		public  function getDB(){ 
			try{
				$dsn = DB_TYPE.":host=".DB_HOST.";dbname=".DB_NAME;
				$this->dbh = new PDO($dsn, DB_USER, DB_PWD); //初始化一个PDO对象
				return  $this->dbh ;
			} catch (PDOException $e) {
				die ("Error!: " . $e->getMessage() . "<br/>");
			}
		}
//----------


//----------


	//----------

	//----------

	//----------


function get_id($name){
	static $id =  0;
	static $TABLE_NAME =  '' ;
	if ( $TABLE_NAME == $name){
 			return $id;
	}else{
		 $TABLE_NAME = $name;
		  return ++$id;
	}
}

function index (){


try{



// $dsn = DB_TYPE.":host=".DB_HOST.";dbname=".DB_NAME;
// $keys = ['PRI'=>'主键','MUL'=>'普通的b-tree索引','UNI'=>'唯一索引' ];

$htmls = [];
//$htmls[] = "<h1 class='pagetitle'>数据字典 Dictionaries</h1>";

// try {
 //   $dbh = new PDO($dsn, DB_USER, DB_PWD); //初始化一个PDO对象


   // $htmls[] = "<p class='pagetitle' >连接成功 Connection succeeded </p>";













	// 数据字典 -- 索引信息
	// $sql="SELECT a.*,
	// MAX(a.NON_UNIQUE)    NON_UNIQUE,
	// MAX(a.INDEX_TYPE)    INDEX_TYPE,
	// MAX(a.INDEX_COMMENT)    INDEX_COMMENT,
	// GROUP_CONCAT(a.COLUMN_NAME)    COLUMN_NAME
	// FROM (SELECT
	// TABLE_SCHEMA,
	// TABLE_NAME,
	// NON_UNIQUE,
	// INDEX_NAME,
	// SEQ_IN_INDEX,
	// COLUMN_NAME,
	// INDEX_TYPE,
	// CONCAT(COMMENT,INDEX_COMMENT)    INDEX_COMMENT
	// FROM INFORMATION_SCHEMA.STATISTICS
	// WHERE TABLE_SCHEMA = '".DB_NAME."'
	// ORDER BY TABLE_SCHEMA,TABLE_NAME,INDEX_NAME,SEQ_IN_INDEX) a
	// GROUP BY a.TABLE_SCHEMA,a.TABLE_NAME,a.INDEX_NAME  ";

	//echo  $sql; exit;
// $all_table_sql= "SELECT TABLE_NAME  FROM INFORMATION_SCHEMA.TABLES  WHERE  TABLE_SCHEMA = '".DB_NAME."' " ;
// $all_table_row =$this->getDB()->query($all_table_sql);
// var_dump($all_table_row  ); exit;

// echo "<p>22</p>";
 //	$index_sql= "SELECT *  FROM INFORMATION_SCHEMA.STATISTICS  WHERE TABLE_SCHEMA = '".DB_NAME."' AND  TABLE_NAME = '".$row['TABLE_NAME']."'    ";
  	$index_sql= "SELECT *  FROM INFORMATION_SCHEMA.STATISTICS  WHERE TABLE_SCHEMA = '".DB_NAME."'     ";	
 
 	$index_row =$this->getDB()->query($index_sql);
  // var_dump(  	$index_row  ); exit;

	$index_info = [];
 foreach ($index_row   as $key => $row) {

 
   
$index_info[$row['TABLE_NAME']][$row['INDEX_NAME']]['TABLE_SCHEMA'] = $row['TABLE_SCHEMA'];
$index_info[$row['TABLE_NAME']][$row['INDEX_NAME']]['NON_UNIQUE'] = $row['NON_UNIQUE'];


$index_info[$row['TABLE_NAME']][$row['INDEX_NAME']]['TABLE_NAME'] = $row['TABLE_NAME'];

if(isset( $index_info[$row['TABLE_NAME']][$row['INDEX_NAME']]['COLUMN_NAME'] )){ 
	$index_info[$row['TABLE_NAME']][$row['INDEX_NAME']]['COLUMN_NAME'] = $index_info[$row['TABLE_NAME']][$row['INDEX_NAME']]['COLUMN_NAME'] . '   ,   '. $row['COLUMN_NAME'];
}else{
	$index_info[$row['TABLE_NAME']][$row['INDEX_NAME']]['COLUMN_NAME'] =  $row['COLUMN_NAME'];
}
$index_info[$row['TABLE_NAME']][$row['INDEX_NAME']]['INDEX_SCHEMA'] = $row['INDEX_SCHEMA'];
$index_info[$row['TABLE_NAME']][$row['INDEX_NAME']]['INDEX_NAME'] = $row['INDEX_NAME'];
$index_info[$row['TABLE_NAME']][$row['INDEX_NAME']]['SEQ_IN_INDEX'] = $row['SEQ_IN_INDEX'];

$index_info[$row['TABLE_NAME']][$row['INDEX_NAME']]['COLLATION'] = $row['COLLATION'];
$index_info[$row['TABLE_NAME']][$row['INDEX_NAME']]['CARDINALITY'] = $row['CARDINALITY'];
$index_info[$row['TABLE_NAME']][$row['INDEX_NAME']]['SUB_PART'] = $row['SUB_PART'];
$index_info[$row['TABLE_NAME']][$row['INDEX_NAME']]['PACKED'] = $row['PACKED'];
$index_info[$row['TABLE_NAME']][$row['INDEX_NAME']]['NULLABLE'] = $row['NULLABLE'];
$index_info[$row['TABLE_NAME']][$row['INDEX_NAME']]['INDEX_TYPE'] = $row['INDEX_TYPE'];
$index_info[$row['TABLE_NAME']][$row['INDEX_NAME']]['COMMENT'] = $row['COMMENT'];
$index_info[$row['TABLE_NAME']][$row['INDEX_NAME']]['INDEX_COMMENT'] = $row['INDEX_COMMENT'];
$index_info[$row['TABLE_NAME']][$row['INDEX_NAME']]['IS_VISIBLE'] = $row['IS_VISIBLE'];
$index_info[$row['TABLE_NAME']][$row['INDEX_NAME']]['EXPRESSION'] = $row['EXPRESSION'];


 
 	 
 }
	 
 


// $sql = "SHOW TABLES";
$tables = [];
$sql = "SELECT  *  FROM information_schema.COLUMNS WHERE  TABLE_SCHEMA='".DB_NAME."'   ";
 //$sql="SELECT TABLE_NAME,TABLE_ROWS,TABLE_COMMENT FROM information_schema.TABLES WHERE table_schema='".DB_NAME."'";
$index = 0;

 
$lists_row =$this->getDB()->query($sql);
 
foreach ($lists_row  as $key => $row) {   
		$tables[$row['TABLE_NAME']]['id'] = $this->get_id( $row['TABLE_NAME'] );     	
 		$tables[$row['TABLE_NAME']]['table_name'] = $row['TABLE_NAME'];
		$tables[$row['TABLE_NAME']]['table_title'] = '';
		$tables[$row['TABLE_NAME']]['lists'][] = $row;
		$tables[$row['TABLE_NAME']]['index_info'] = $index_info[$row['TABLE_NAME']] ?? [] ;		

 

}

 

//TABLE_NAME,TABLE_COMMENT 
 $sql="SELECT TABLE_NAME,TABLE_ROWS,TABLE_COMMENT FROM information_schema.TABLES WHERE table_schema='".DB_NAME."'  ";
// $rows=  $dbh->query($sql);
$rows= $this->getDB()->query($sql);

foreach ($rows  as $key => $row) {   
			$tables[$row['TABLE_NAME']]['table_title'] = $row['TABLE_COMMENT']; 
			$tables[$row['TABLE_NAME']]['table_rows'] = $row['TABLE_ROWS']; 
			
 }


  echo join('',$htmls);

$htmls = [];
foreach ($tables as $key => $row) {	  
 
		$htmls[] = "<p class='table_name' id='table_id_".$row['id']."'>";
		$htmls[] = "<span class='table_name-id'>".$row['id']."</span>"; 
		$htmls[] = "<span class='table_name-name'>".$row['table_name'] ."</span>"; 
		$htmls[] = "<span class='table_name-title'> ".$row['table_title']."</span>"; 
		$htmls[] = "<span class='table_name-rows'> <font>".$row['table_rows']."</font>条记录</span>";
		$htmls[] = "</p>"; 
		$htmls[] = "<div class='table_lists'><table width='100%' border='0' cellspacing='1' cellpadding='0' >";
		$htmls[] = "<tr>";
		$htmls[] = "<th>字段名 </th>";
		$htmls[] = "<th>数据类型 </th>";
		$htmls[] = " <th>是否为空</th>";
		$htmls[] = " <th>键</th>";
		$htmls[] = " <th>默认值</th>";
		$htmls[] = " <th>其它的</th>"; 		
		$htmls[] = " <th>列注释</th>";  
		$htmls[] = " <th>字符集</th>"; 		
		$htmls[] = " <th>排序规则</th>"; 
		$htmls[] = "</tr>";	  
		foreach ($row['lists'] as $k => $r) {
				$htmls[] = "<tr>";
				$htmls[] = "<td class='COLUMN_NAME'> {$r['COLUMN_NAME']} </td>";
				$htmls[] = "<td class='COLUMN_TYPE' >  {$r['COLUMN_TYPE']  }</td>";				
				$htmls[] = "<td class='IS_NULLABLE' > {$r['IS_NULLABLE']} </td>";  		   
				$htmls[] = "<td class='COLUMN_KEY' >  ".  (isset($this->keys[$r['COLUMN_KEY']]) ? $this->keys[$r['COLUMN_KEY']] : '')  . "</td>";			
				$htmls[] = "<td class='COLUMN_DEFAULT' > {$r['COLUMN_DEFAULT']} </td>";		
				$htmls[] = "<td class='EXTRA' > {$r['EXTRA']} </td>";					
				$htmls[] = "<td class='COLUMN_COMMENT' > {$r['COLUMN_COMMENT']} </td>";
				$htmls[] = "<td class='CHARACTER_SET_NAME' > {$r['CHARACTER_SET_NAME']} </td>";
				$htmls[] = "<td class='COLLATION_NAME' > {$r['COLLATION_NAME']} </td>";
				$htmls[] = "</tr>";
		}
		$htmls[] = "</table>";
		$htmls[] = "</div>";
		
		
		$htmls[] = "<div class='table_index'><table width='100%' border='0' cellspacing='1' cellpadding='0' >";
		$htmls[] = "<tr>";
		$htmls[] = " <th class='index' >索引名</th>";
		$htmls[] = "<th class='index'>索引字段名 </th>";
		$htmls[] = "<th class='index' >索引类型 </th>";
		
		$htmls[] = " <th class='index' >唯一</th>";
		$htmls[] = " <th class='index' >索引注释</th>";  
				$htmls[] = " <th class='index' >EXPRESSION</th>";  
		$htmls[] = "</tr>";	  
		foreach ($row['index_info'] as $k => $r) {			
			$htmls[] = "<tr>"; 				

// $index_info[$row['TABLE_NAME']][$row['INDEX_NAME']]['TABLE_SCHEMA'] = $row['TABLE_SCHEMA'];
// $index_info[$row['TABLE_NAME']][$row['INDEX_NAME']]['NON_UNIQUE'] = $row['NON_UNIQUE'];


// $index_info[$row['TABLE_NAME']][$row['INDEX_NAME']]['TABLE_NAME'] = $row['TABLE_NAME'];

// if(isset( $index_info[$row['TABLE_NAME']][$row['INDEX_NAME']]['COLUMN_NAME'] )){ 
// 	$index_info[$row['TABLE_NAME']][$row['INDEX_NAME']]['COLUMN_NAME'] = $index_info[$row['TABLE_NAME']][$row['INDEX_NAME']]['COLUMN_NAME'] . '   ,   '. $row['COLUMN_NAME'];
// }else{
// 	$index_info[$row['TABLE_NAME']][$row['INDEX_NAME']]['COLUMN_NAME'] =  $row['COLUMN_NAME'];
// }
// $index_info[$row['TABLE_NAME']][$row['INDEX_NAME']]['INDEX_SCHEMA'] = $row['INDEX_SCHEMA'];
// $index_info[$row['TABLE_NAME']][$row['INDEX_NAME']]['INDEX_NAME'] = $row['INDEX_NAME'];
// $index_info[$row['TABLE_NAME']][$row['INDEX_NAME']]['SEQ_IN_INDEX'] = $row['SEQ_IN_INDEX'];

// $index_info[$row['TABLE_NAME']][$row['INDEX_NAME']]['COLLATION'] = $row['COLLATION'];
// $index_info[$row['TABLE_NAME']][$row['INDEX_NAME']]['CARDINALITY'] = $row['CARDINALITY'];
// $index_info[$row['TABLE_NAME']][$row['INDEX_NAME']]['SUB_PART'] = $row['SUB_PART'];
// $index_info[$row['TABLE_NAME']][$row['INDEX_NAME']]['PACKED'] = $row['PACKED'];
// $index_info[$row['TABLE_NAME']][$row['INDEX_NAME']]['NULLABLE'] = $row['NULLABLE'];
// $index_info[$row['TABLE_NAME']][$row['INDEX_NAME']]['INDEX_TYPE'] = $row['INDEX_TYPE'];
// $index_info[$row['TABLE_NAME']][$row['INDEX_NAME']]['COMMENT'] = $row['COMMENT'];
// $index_info[$row['TABLE_NAME']][$row['INDEX_NAME']]['INDEX_COMMENT'] = $row['INDEX_COMMENT'];
// $index_info[$row['TABLE_NAME']][$row['INDEX_NAME']]['IS_VISIBLE'] = $row['IS_VISIBLE'];
// $index_info[$row['TABLE_NAME']][$row['INDEX_NAME']]['EXPRESSION'] = $row['EXPRESSION'];



			$htmls[] = "<td class='index-INDEX_NAME'> ".$r['INDEX_NAME'] ." </td>";
			$htmls[] = "<td class='index-COLUMN_NAME'> ".$r['COLUMN_NAME'] ." </td>"; 	
			$htmls[] = "<td class='index-INDEX_TYPE'> ".$r['INDEX_TYPE'] ." </td>";

			$htmls[] = "<td class='index-NON_UNIQUE'> ".$r['NON_UNIQUE'] ." </td>";
			$htmls[] = "<td class='index-INDEX_COMMENT'> ".$r['INDEX_COMMENT'] ." </td>";
			$htmls[] = "<td class='index-EXPRESSION'> ".$r['EXPRESSION'] ." </td>";
			$htmls[] = "</tr>";	
		}
		$htmls[] = "</table>";
		$htmls[] = "</div>";

echo join('',$htmls);
$htmls = [];
	
}

		

$htmls = [];
$htmls[] = "    <div  onclick='onclicknav()'  id='navbut'   >关闭导航</div> ";
$htmls[] = "<div  id='nav' class='nav' style='height:100%;'>";
$htmls[] = "<p  >数据字典 Dictionaries </p> ";

$htmls[] = '<p  >' . count($tables) . '个数据表</p> '; 
$htmls[] = '<p  >登录user:  '.$_SESSION['username'].'  <a href="?ac=logout&t='.time().'" >[ logout ] </a>   </p> ';
$htmls[] = '<p  >登录时间:   '. date('Y-m-d H:i:s', $_SESSION['login_time'] ) .' </p> ';
 


$htmls[] = "<ul>";
foreach ($tables as $key => $row) {	  
		$htmls[] = "<li title='".$row['table_name']."'><a href='#table_id_".$row['id']."'>";
		$htmls[] = $row['id']."  ".$row['table_name'];
		$htmls[] = "</a></li>";
}
$htmls[] = "</ul>";
$htmls[] = "</div>";

echo join('',$htmls);
 // $dbh = null;
 $this->dbh = null;





//echo join('',$htmls);
} catch (PDOException $e) {
				die ("Error!: " . $e->getMessage() . "<br/>");
			}

}




 //ac=logout
function logout( ){ 
		 				unset( $_SESSION['login_time']);
		 				unset( $_SESSION['username']);
		 				unset( $_SESSION['password']);	
		 				 echo "<p>logout 成功.... </p> ";
  						echo "<p> <a href='?ac=login&t=".time()." ' >goto login .... </a> </p> ";
  						sleep(2);
  					 
  						 echo "<script> window.location.href = '?ac=login&t=".time()."'; </script>  ";
  							 exit();
}

function login( ){ 

				// 在服务器端生成并存储CSRF令牌  
			$csrfToken = $this->generateCSRFToken();  
			$_SESSION['csrfToken'] = $csrfToken;  

			    // 用户未登录，执行登录前的操作  
			    // ...  
			     echo "<p> 请登录 </p> ";
			echo ' <h2>登录 '.date('Y-m-d H:i:s', time() ) .'</h2>  ';
			echo '<form action="?ac=login_sub&t='.time()  .'" method="post">  ';

			echo ' <label for="username">用户名:</label>  ';
			echo ' <input type="text" id="username" name="username" required><br><br>  ';
			echo ' <label for="password">密&nbsp;&nbsp;&nbsp;码:</label>  ';
			echo ' <input type="password" id="password" name="password" required><br><br>  ';
			echo ' <input type="submit" value="登录" style="padding:10px; width:300px;">  ';

			echo ' <input type="hidden" name="csrfToken" value="'.$csrfToken.'"> ';
			 

			echo ' </form>  ';
			 echo "<p> <a href='?ac=login&t=".time()  ."' >[ f5 login ] </a> </p> ";
 exit();
}

function login_sub( ){ 
 			if ($_SERVER['REQUEST_METHOD'] === 'POST') {  
 

					// 获取提交的CSRF令牌  
					$submittedCsrfToken = $_POST['csrfToken'];  
					  
					// 检查提交的CSRF令牌是否与存储在会话中的令牌匹配  
					if ($submittedCsrfToken === $_SESSION['csrfToken'] ) {  
					    // 允许提交  
					    echo "<p>请稍等.... </p> ";
					    					    // 获取POST请求的数据  
					    $username = $_POST['username'];  
					    $password = $_POST['password'];  

					    if( $_POST['username'] === $this->user_array['username'] &&  $_POST['password'] === $this->user_array['password']   ){
					    		$_SESSION['username'] =  $this->user_array['username']; //list($fruit1, $fruit2, $fruit3) = $fruits;  
					    		$_SESSION['password'] =  $this->user_array['password']; 
					    		$_SESSION['login_time'] =  time(); 
					   
						        echo "<p>登录成功.... </p> ";
								echo "<p> <a href='?ac=index&t=".time()."' > join </a> </p> ";
								echo "<script> window.location.href = '?ac=index&t=".time()."'; </script>  ";


								exit();

							 }else{
							 		 unset( $_SESSION['login_time']);
					 				unset( $_SESSION['username']);
					 				unset( $_SESSION['password']);	

							 			 echo "<p>登录失败.... </p> ";
										echo "<p> <a href='?ac=login&t=".time()."' >re login </a> </p> ";
										sleep(2);
										//exit(99);
											echo "<script> window.location.href = '?ac=login&t=".time()."'; </script>  ";
										exit();
							 }


					} else {  

						  echo "<p> <a href='?ac=login&t=".time()."' >f5</a> </p> ";
							//echo  $submittedCsrfToken ;
						  echo "<p> <a href='?ac=login&t=".time()."' >f5</a> </p> ";
						 // echo $_SESSION['csrfToken'] ;
						   echo "<p> <a href='?ac=login&t=".time()."' >f5</a> </p> ";
						   sleep(2);
						     	echo "<script>window.location.reload(true);</script>";
					    // 拒绝提交，可能是远程提交或CSRF攻击  
					    exit('拒绝提交，可能是远程提交或CSRF攻击 ');
					}

}else{
					 exit("use POST method  REQUEST plese! ");
}
  exit();

 }



//class end 
 }

?>






 
<script src="nav.js?t=wwwwwwsssssswssssss12aa3456y">

	
</script>





</body>
</html>


